﻿using System.Data;
using Newtonsoft.Json;

namespace UFIDA.U9.WSS.ProxyService.Json
{
    /// <summary>
    ///     DataSet转换器
    ///     1、保留定义的表名
    /// </summary>
    public class DataSetConverter : Newtonsoft.Json.Converters.DataSetConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            DataSet dataSet = (DataSet) value;
            //DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
            DataTableConverter converter = new DataTableConverter();
            writer.WriteStartObject();
            foreach (DataTable table in dataSet.Tables)
            {
                //writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(table.TableName) : table.TableName);
                writer.WritePropertyName(table.TableName);
                converter.WriteJson(writer, table, serializer);
            }

            writer.WriteEndObject();
        }
    }
}